package com.mybatis.jpa.SqlProvider;

import java.util.HashMap;

/**
 * @Author lgt
 * @Date 2017/10/10 : 21:21
 */
public class Condition extends HashMap<String, Object> {

    private StringBuffer condition;

    private OrderBy orderBy;

    public Condition() {
        this.condition = new StringBuffer();
    }

    public Condition where(){
        this.condition.append(" WHERE ");
        return this;
    }

    public Condition and(){
        this.condition.append(" AND ");
        return this;
    }

    public Condition or(){
        this.condition.append(" OR ");
        return this;
    }

    public Condition open() {
        this.condition.append(" ( ");
        return this;
    }

    public Condition isNull(String column) {
        this.condition.append(" ").append(column).append(" IS NULL ");
        return this;
    }

    public Condition isNotNull(String column) {
        this.condition.append(" ").append(column).append(" IS NOT NULL  ");
        return this;
    }

    public Condition end() {
        this.condition.append(" ) ");
        return this;
    }

    public Condition equal(String column, Object value) {
        this.condition.append(column).append(" = #{").append(column).append("} ");
        put(column, value);
        return this;
    }

    public Condition like(String column, Object value) {
        this.condition.append(column).append(" LIKE #{").append(column).append("} ");
        put(column, "%"+value+"%");
        return this;
    }

    public Condition orderBy(OrderBy orderBy) {
        this.condition.append(orderBy.toString());
        return this;
    }

    public Condition lt(String column, Object value) {
        this.condition.append(column).append(" < ").append(value);
        put(column, value);
        return this;
    }

    public Condition le(String column, Object value) {
        this.condition.append(column).append(" <= ").append(value);
        put(column, value);
        return this;
    }

    public Condition gt(String column, Object value) {
        this.condition.append(column).append(" > ").append(value);
        put(column, value);
        return this;
    }

    public Condition ge(String column, Object value) {
        this.condition.append(column).append(" >= ").append(value);
        put(column, value);
        return this;
    }

    public Condition limit(int begin, int size) {
        this.condition.append(" LIMIT #{begin},#{size}");
        put("begin", begin);
        put("size", size);
        return this;
    }

    public Condition groupBy(String column) {
        this.condition.append(" GROUP BY ").append(column);
        return this;
    }

    public void setUpdateLoad(Object load) {
        put("updateLoad", load);
    }

    public Object getUpdateLoad(){
        return get("updateLoad");
    }

    @Override
    public String toString() {
        return condition.toString();
    }

}
